//
//  YYFixItemViewController.m
//  YYSliderTabProject
//
//  Created by 蓓蕾 on 2021/8/19.
//

#import "YYFixItemViewController.h"
#import "DetailViewController.h"

/** 选中标题颜色 */
#define SelectTextColor [UIColor redColor]
/** 为选中标题颜色 */
#define NormalTextColor [UIColor blackColor]
/** 滑块颜色 */
#define SliderColor [UIColor redColor]
/** 标题个数 */
static NSInteger const TitleCount = 3;
/** 每个标签的宽度 */
static NSInteger const itemWidth = 80;

@interface YYFixItemViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSArray *titleArr;// 标题数据

/** 标题视图 */
@property (nonatomic, strong) UIView *titleView;
/** 内容视图 */
@property (nonatomic, strong) UIScrollView *contentScrollView;
/** 指示条 */
@property (nonatomic, strong) UIView *sliderView;

@end

@implementation YYFixItemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 定义临时变量
    _titleArr = @[@"未开票", @"申请中", @"已开票"];

    [self setUI];
}

-(void)setUI
{
    _contentScrollView = [[UIScrollView alloc] init];
    _contentScrollView.showsVerticalScrollIndicator = NO;
    _contentScrollView.showsHorizontalScrollIndicator = NO;
    _contentScrollView.delegate = self;
    _contentScrollView.pagingEnabled = YES;
    _contentScrollView.frame = CGRectMake(0, 100, kScreenWidth, kScreenHeight - 64);
    [self.view addSubview:self.contentScrollView];
    _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, kScreenWidth, 40)];
    [self.view addSubview:_titleView];
    _titleView.backgroundColor = [UIColor whiteColor];
    // 添加子控制器
    [self setupChildVc];
    // 添加标题
    [self setupTitle];
    // 默认显示第0个子控制器
    [self scrollViewDidEndScrollingAnimation:self.contentScrollView];

    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = _currentIndex * self.contentScrollView.frame.size.width;
    [self.contentScrollView setContentOffset:offset animated:YES];
}

/** 添加标题 */
- (void)setupTitle
{
    CGFloat itemSpace = (kScreenWidth - itemWidth * TitleCount) / (TitleCount + 1);
    NSLog(@"itemSpace = %lf",itemSpace);
    // 添加label
    for (NSInteger i = 0; i < _titleArr.count; i++) {
        UILabel *label = [[UILabel alloc] init];

        label.text = _titleArr[i];
        label.userInteractionEnabled = YES;
        label.frame = CGRectMake(itemSpace + i * (itemWidth + itemSpace), 0, itemWidth, CGRectGetHeight(_titleView.bounds));
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)]];
        label.tag = i;
        label.textAlignment = NSTextAlignmentCenter;
        [self.titleView addSubview:label];
    }
    self.contentScrollView.contentSize = CGSizeMake(_titleArr.count * kScreenWidth, 0);

    // 底部的红色指示器
    _sliderView = [[UIView alloc] init];
    _sliderView.backgroundColor = SliderColor;
    [self.titleView addSubview:_sliderView];
}
/** 设置ChildVC */
- (void)setupChildVc
{
    for (NSInteger i = 0; i < _titleArr.count; ++i) {
        DetailViewController *detailVC = [[DetailViewController alloc] init];
        detailVC.index = i;
        [self addChildViewController:detailVC];
    }
}
/** 监听顶部label点击 */
- (void)labelClick:(UITapGestureRecognizer *)tap
{
    // 取出被点击label的索引
    NSInteger index = tap.view.tag;
    // 让底部的内容scrollView滚动到对应位置
    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = index * self.contentScrollView.frame.size.width;
    [self.contentScrollView setContentOffset:offset animated:YES];
}


#pragma mark - <UIScrollViewDelegate>
/**
 * scrollView结束了滚动动画以后就会调用这个方法（比如- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;方法执行的动画完毕后）
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 一些临时变量
    CGFloat width = scrollView.frame.size.width;
    CGFloat height = scrollView.frame.size.height;
    CGFloat offsetX = scrollView.contentOffset.x;

    // 当前位置需要显示的控制器的索引
    NSInteger index = offsetX / width;

    // 让对应的顶部标题居中显示
    UILabel *label = self.titleView.subviews[index];
    label.textColor = SelectTextColor;
//    [UIView animateWithDuration:0.25 animations:^{
        self.sliderView.frame = CGRectMake(CGRectGetMinX(label.frame), CGRectGetMaxY(label.frame), CGRectGetWidth(label.frame), 2);
//    }];
    // 让其他label回到最初的状态
    for (UILabel *otherLabel in self.titleView.subviews) {
        if ([otherLabel isKindOfClass:[UILabel class]]) {
            if (otherLabel != label) {
                otherLabel.textColor = NormalTextColor;
            }
        }
    }

    // 取出需要显示的控制器
    UIViewController *willShowVc = self.childViewControllers[index];

    // 如果当前位置的位置已经显示过了，就直接返回
    if ([willShowVc isViewLoaded]) return;

    // 添加控制器的view到contentScrollView中;
    willShowVc.view.frame = CGRectMake(offsetX, 0, width, height);
    [scrollView addSubview:willShowVc.view];
}

/** 手指松开scrollView后，scrollView停止减速完毕就会调用这个 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

/** 只要scrollView在滚动，就会调用 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat itemSpace = (kScreenWidth - itemWidth * TitleCount) / (TitleCount + 1);
    CGFloat contentOffSetX = scrollView.contentOffset.x;
    CGRect frame = _sliderView.frame;
    frame.origin.x = (contentOffSetX/kScreenWidth + 1) * itemSpace + (contentOffSetX/kScreenWidth * itemWidth);
    _sliderView.frame = frame;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
